package com.yuanyi.liu;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;

public class DrawLineView extends View{
	private Paint mPaint;
	private Bitmap mBitmap;
	private float x1 = 0;
	private float y1 = 0;
	private ArrayList<MyRect> lines;
	private boolean isMove = false;
	private boolean changeSize1 = false;
	private boolean changeSize2 = false;
	public DrawLineView(Context context) {
		super(context);
		
		mPaint = new Paint();
		mPaint.setAntiAlias(true);
		mPaint.setARGB(0, 0xff, 0, 0);
		mBitmap = Bitmap.createBitmap(480, 800, Bitmap.Config.RGB_565);
		lines = new ArrayList<MyRect>();
	}
	
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		if(mBitmap != null)
			canvas.drawBitmap(mBitmap, 0, 0,null);
	}
	
	public boolean onTouchEvent(MotionEvent event){
		int x = 0;
		int y = 0;
		x = (int)event.getX();
		y = (int)event.getY();
		Canvas canvas = new Canvas();
		canvas.setBitmap(mBitmap);
		mPaint.setARGB(200, 0xff, 0, 0);
		
		canvas.drawColor(Color.BLACK);
		switch(event.getAction()){
			case MotionEvent.ACTION_DOWN:{
				//没有直线就创建直线，否则判定是移动还是改变直线大小
				if(lines.size()==0){
					canvas.drawLine(x-30, y-30, x+30, y+30, mPaint);
					invalidate(0,0,480,800);
					lines.add(new MyRect(x-30, y-30, x+30, y+30));
				}else{
					int mid_x, mid_y;
					for(int i = 0; i < lines.size(); i++){
						MyRect line = lines.get(i);
						mid_x = (line.x1 + line.x2)/2;
						mid_y = (line.y1 + line.y2)/2;
						if(x>mid_x-30 && x<mid_x+30
								&&y>mid_y-30 && y<mid_y+30){
							isMove = true;
							break;
						}else if(x>line.x1-20 && x<line.x1+20 
								&& y>line.y1-20 && y<line.y1+20){
							changeSize1 = true;
							break;
						}else if(x>line.x2-20 && x<line.x2+20 
								&& y>line.y2-20 && y<line.y2+20){
							changeSize2 = true;
							break;
						}
					}
				}
				break;
			}
			case MotionEvent.ACTION_MOVE:{
				if(lines.size()!=0){
					if(isMove){
						MyRect line = lines.get(0);
						int mid_x = (line.x1 + line.x2)/2;
						int mid_y = (line.y1 + line.y2)/2;
						int move_x = x - mid_x;
						int move_y = y - mid_y;
						line.x1 += move_x;
						line.x2 += move_x;
						line.y1 += move_y;
						line.y2 += move_y;
						canvas.drawLine(line.x1, line.y1, line.x2, line.y2, mPaint);
						invalidate(0,0,480,800);
					}
					
					if(changeSize1){
						MyRect line = lines.get(0);
						line.x1 = x;
						line.y1 = y;
						canvas.drawLine(line.x1, line.y1, line.x2, line.y2, mPaint);
						invalidate(0,0,480,800);
					}
					
					if(changeSize2){
						MyRect line = lines.get(0);
						line.x2 = x;
						line.y2 = y;
						canvas.drawLine(line.x1, line.y1, line.x2, line.y2, mPaint);
						invalidate(0,0,480,800);
					}
				}
				break;
			}
			case MotionEvent.ACTION_UP:{
				isMove = false;
				changeSize1 = false;
				changeSize2 = false;
				break;
			}
		}
		return true;
	}

}

class MyLine{
	
	public MyLine(int x1, int y1, int x2, int y2){
		this.x1 = x1;
		this.x2 = x2;
		this.y1 = y1;
		this.y2 = y2;
	}
	int x1,x2,y1,y2;
}
